<?php
/**
 * @title 97. 交错字符串 - 递归
 * @author start2004
 */

class Solution {

    /**
     * @param String $s1
     * @param String $s2
     * @param String $s3
     * @return Boolean
     */
    function isInterleave($s1, $s2, $s3) {
        /**
         * @since 2020-07-20 长度
         */
        $len1 = strlen($s1);
        $len2 = strlen($s2);
        $len3 = strlen($s3);
        if($len1+$len2 != $len3){
            return false;
        } else {}

        /**
         * @return
         */
        return self::find($s1, $s2, $s3, 0, 0, 0, $len1, $len2, $len3);
    }

    /**
     * @title 检查是否交错
     * @author start2004
     * @since 2020-07-21 Created
     *
     * @param string $s1
     * @param string $s2
     * @param string $s3
     * @param int $p1
     * @param int $p2
     * @param int $p3
     * @param int $len1
     * @param int $len2
     * @param int $len3
     * @return bool
     */
    function find($s1='', $s2='', $s3='', $p1=0, $p2=0, $p3=0, $len1=0, $len2=0, $len3=0){
        /**
         * @since 2020-07-21 `s1`匹配结束
         */
        if($p1 == $len1){
            /**
             * @since 2020-07-21 检查`s2`剩余和`s3`剩余是否一致
             */
            if(substr($s2, $p2) == substr($s3, $p3)){
                return true;
            } else {
                return false;
            }
        } else {}

        /**
         * @since 2020-07-21 `s2`匹配结束
         */
        if($p2 == $len2){
            /**
             * @since 2020-07-21 检查`s1`剩余和`s3`剩余是否一致
             */
            if(substr($s1, $p1) == substr($s3, $p3)){
                return true;
            } else {
                return false;
            }
        } else {}

        /**
         * @since 2020-07-21 尝试`s1`匹配
         */
        if($s3[$p3] == $s1[$p1]){
            $result = self::find($s1, $s2, $s3, $p1+1, $p2, $p3+1, $len1, $len2, $len3);
            if($result){
                return true;
            } else {}
        } else {}

        /**
         * @since 2020-07-21 尝试`s2`匹配
         */
        
        if($s3[$p3] == $s2[$p2]){
            $result = self::find($s1, $s2, $s3, $p1, $p2+1, $p3+1, $len1, $len2, $len3);
            if($result){
                return true;
            } else {}
        }

        /**
         * @return
         */
        return false;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.18.php
 */
$datas = [
    ["aabcc", "dbbca", "aadbbcbcac"],
    ["aabcc", "dbbca", "aadbbbaccc"],
    // ["bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa", "babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab", "babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->isInterleave($data[0], $data[1], $data[2]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
